<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>摄像机列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.6.3/css/layui.css" media="all" >
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/camera/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {field: 'id', width: 100, title: 'ID'},
                {field: 'bindJointid', width: 200, title: '接头ID'},
                {field: 'cameraId', width: 200, title: '球机ID'},
                {field: 'cameraName', width: 200, title: '球机名称'},
                {field: 'ip', width: 120, title: 'IP'},
                {field: 'port', width: 120, title: '端口'},
                {field: 'username', width: 120, title: '用户名'},
                {field: 'password', width: 120, title: '密码'},
                {field: 'enable', width: 120, title: '是否自动',
                    templet: function(d) {
                        if (d.enable === 0) {
                            return '自动';
                        } else if (d.enable === 1) {
                            return '手动';
                        } else {
                            return "";
                        }
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加摄像机',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    content: '/camera/add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var id = obj.data.id;
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑摄像机信息',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    content: '/camera/edit?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除？', function (index) {
                    layer.close(index);
                    removeIds(id);
                });
            }
        });

        function removeIds(arr) {
            $.ajax({
                url:  "/camera/remove",    //请求的url地址
                dataType: "json",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                data: {
                    id: arr
                },    //参数值,键值对
                type: "POST",   //请求方式
                success: function (result) {
                    if (result.code == 0) {
                        refreshTable();
                    }else {
                        layer.msg(result.msg);
                    }
                },
                error: function () {
                    return false;
                }
            });
        }

        function refreshTable() {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                }
            }, 'data');
        }

    });

</script>

</body>
</html>